package com.gzhryc.shared_device.oem.code.place.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.place.dao.ClerkDao;
import com.gzhryc.shared_device.oem.code.place.dao.db.Clerk;
import com.gzhryc.shared_device.oem.code.place.services.dto.ClerkSearch;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.code.user.services.UserService;

public class ClerkService {

	static Logger log = Logger.getLogger(ClerkService.class);

    public static ClerkService self() {
        return MultiDBTools.getService(ClerkService.class, true);
    }

    public static ClerkService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, ClerkService.class, true);
    }

    ClerkDao dao;

    public ClerkService(String jdbcKey) {
        this.dao = new ClerkDao(jdbcKey);
    }
    
    public List<Clerk> findJoinBySearch(ClerkSearch search, Integer pageNo, Integer pageLimit) {
        Conditions conditions = dao.buildConditions(search);
        conditions.and().likeBoth("user.name", search.getTrim())
                .or().likeBoth("user.phone", search.getTrim());

        try {
            return dao.findJoinByCondition(conditions, pageNo, pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public Long countJoinBySearch(ClerkSearch search){
        Conditions conditions = dao.buildConditions(search);
        conditions.and().likeBoth("user.name", search.getTrim())
                .or().likeBoth("user.phone", search.getTrim());

        try {
            return dao.countJoin(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return 0L;
    }
    
    public List<Clerk> findByUserId(Long userId) {
    	if(userId != null) {
	    	Conditions conditions = new Conditions(Clerk.class);
	        conditions.and().is("userId", userId);
	
	        try {
	            return dao.findByCondition(conditions);
	        } catch (SQLException e) {
	            log.error(e.getMessage(), e);
	        }
    	}
        return null;
    }

    public List<Clerk> findJoinByMerchantUserId(Long merchantUserId,Integer pageNo,Integer pageLimit){
        if(merchantUserId != null){
            Conditions conditions = new Conditions(Clerk.class);
            conditions.and().is("merchantUserId",merchantUserId);

            try {
                return dao.findJoinByCondition(conditions,pageNo,pageLimit);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }
    
    public Clerk getByLastLogin(Long userId) {
    	if(userId != null) {
    		Conditions conditions = new Conditions(Clerk.class);
            conditions.and().is("userId",userId);
            conditions.desc("lastLoginDate");
            
            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
    	}
    	return null;
    }
    
    public boolean replaceLastLogin(Long userId,Long merchantUserId) {
    	if(userId != null && merchantUserId != null) {
    		Conditions conditions = new Conditions(Clerk.class);
            conditions.and().is("userId",userId);
            conditions.and().is("merchantUserId",merchantUserId);
            
            Clerk updateEntity = new Clerk();
            updateEntity.setLastLoginDate(new Date());
            
            try {
				return dao.updateNotNull(updateEntity, conditions) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
    	}
    	return false;
    }
    
    public Clerk getJoinByKeys(Long userId,Long merchantUserId) {
    	if(userId != null && merchantUserId != null) {
    		Conditions conditions = new Conditions(Clerk.class);
            conditions.and().is("userId",userId);
            conditions.and().is("merchantUserId",merchantUserId);
            
            try {
				return dao.getJoin(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
    	}
    	return null;
    }
    
    public boolean add(Clerk clerk) throws LogicException{
    	clerk.setCreateDate(new Date());
    	try {
			return dao.insert(clerk) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
    	return false;
    }
    
    public boolean add(Clerk clerk,User user) throws LogicException {
    	clerk.setCreateDate(new Date());
    	JdbcSession.begin();
        try {
            if(UserService.self().add(user)){
            	clerk.setUserId(user.getId());
            	clerk.setCreateDate(new Date());
                if(dao.insert(clerk) > 0){
                	 JdbcSession.commit();
                     return true;
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }finally {
            JdbcSession.end();
        }
    	return false;
    }
    
    public boolean updateCanCancelOrder(Long userId,Long merchantUserId,Integer canCancelOrder) {
    	if(userId != null && merchantUserId != null && canCancelOrder != null) {
    		Conditions conditions = new Conditions(Clerk.class);
    		conditions.and().is("userId", userId);
    		conditions.and().is("merchantUserId", merchantUserId);
    		
    		Clerk clerk = new Clerk();
    		clerk.setCanCancelOrder(canCancelOrder);
    		
    		try {
				return dao.updateNotNull(clerk, conditions) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
}
